#include <controller/FileSystemController.h>
#include <cppwinks/utility/json/Json.h>
#include <service/FileSystemService.h>

#include <iostream>
#include <chrono>
#include <random>
#include <sstream>
#include <fstream>

using namespace std;
using namespace json;
using namespace network;

// 上传图片接口
network::HttpReply FileSystemController::uploadImage(const network::HttpRequest &req)
{
    try
    {
        // 获取图片内容
        string imageContent = req.getBody();
        if (imageContent.empty()) {
            cout << "请求体为空" << endl;
            throw runtime_error("请求体为空");
        }
        cout << "Request body size: " << imageContent.length() << endl;
        // 获取图片格式
        string contentType = req.getHeader("Content-Type");
        cout << "contentType" << endl;
        string imageFormat = "jpg";  // 默认格式为 jpg
        if (contentType == "image/png") {
            imageFormat = "png";
        } else if (contentType == "image/jpeg") {
            imageFormat = "jpg";
        } else if (contentType == "image/gif") {
            imageFormat = "gif";
        } else if (contentType == "image/bmp") {
            imageFormat = "bmp";
        } else if (contentType == "image/webp") {
            imageFormat = "webp";
        } else {
            Json ret;
            ret["code"] = 0;
            ret["msg"] = "不支持的图片格式";
            network::HttpReply rep("500");
            rep.setBody(ret.toString());
            return rep;
        }

        // 生成图片文件名（可以根据需求修改）
        auto now = std::chrono::system_clock::now();
        auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();

        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(0, 9999);

        std::stringstream ss;
        ss << timestamp << "_" << dis(gen);
        string s = ss.str();
        string imageName = s + "." + imageFormat;

        // 使用业务层写入图片
        FileSystemService fileSystemService;
        fileSystemService.writeFile(imageName, imageContent);

        // 返回成功响应
        Json ret;
        ret["code"] = 1;
        ret["msg"] = "图片上传成功";
        ret["data"] = imageName;

        network::HttpReply rep("200");
        rep.setBody(ret.toString());
        return rep;
    }
    catch (const std::exception &e)
    {
        cout << "图片上传失败: " << e.what() << cout;
        Json ret;
        ret["code"] = 0;
        ret["msg"] = "图片上传失败";
        network::HttpReply rep("500");
        rep.setBody(ret.toString());
        return rep;
    }
}

// 获取图片接口
network::HttpReply FileSystemController::getImage(const network::HttpRequest &req)
{
    try
    {
        // 获取图片路径
        string avatar_url = req.getUrlParameter().at("avatar_url");
        cout << "Requested file path: " << avatar_url << endl;  // 打印请求的文件路径
        // 使用业务层读取图片内容
        string imageContent = file_system_service->readFile(avatar_url);
        network::HttpReply rep("200");
        rep.addHeader("Content-Type", "image/jpeg");
        rep.setBody(imageContent);
        return rep;
    }
    catch (const std::exception &e)
    {
        cout << "获取图片失败: " << e.what() << cout;
        Json ret;
        ret["code"] = 0;
        ret["msg"] = "获取图片失败";
        network::HttpReply rep("500");
        rep.setBody(ret.toString());
        return rep;
    }
}